/** @odoo-module **/

import { Layout } from "@web/search/layout";
import { registry } from "@web/core/registry";
import { getDefaultConfig } from "@web/views/view";
import { useService } from "@web/core/utils/hooks";
import { Echarts } from "../echarts/echarts";

import { Component, useSubEnv, validate } from "@odoo/owl";

export class EchartsAction extends Component {
    setup() {
        useSubEnv({
            config: {
                ...getDefaultConfig(),
                ...this.env.config,
            },
        });

        this.ormService = useService("orm");
        this.actionParams = this.props.action.params;
        validate(this.actionParams, {
            res_model: String,
            get_echarts_options: String,
            get_echarts_datas: String,
            hasBreadcrumb: { type: Boolean, optional: true }
        });
        this.hasBreadcrumb = this.actionParams.hasBreadcrumb;
        this.resModel = this.actionParams.res_model;
    }

    async getEchartsOptions() {
        return await this.ormService.call(
            this.resModel,
            this.actionParams.get_echarts_options,
            []
        );
    }

    async getEchartsDatas() {
        return await this.ormService.call(
            this.resModel,
            this.actionParams.get_echarts_datas,
            []
        );
    }
}

EchartsAction.template = "echarts_action.EchartsAction";
EchartsAction.components = { Layout, Echarts };

registry.category("actions").add("echarts_action", EchartsAction);